package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.*;
import com.itheima.common.pageParm;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderdetailDao;
import com.itheima.dto.OrdersDto;
import com.itheima.service.*;
import com.itheima.utils.BaseContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Transactional
@Service
public class OrderServiceImpl implements OrderService {

    @Resource

    private OrderdetailDao orderdetailDao;

    @Resource
    private OrderdetailService orderdetailService;

    @Resource
    private HttpSession session;

    @Resource
    private OrderDao orderDao;
    @Autowired
    private ShoppingCartService shoppingCartService;


    @Autowired
    private AddressBookService addressBookService;

    @Override
    public int submit(Orders orders, User user) {

        //往订单表里面传数据

        orders.setNumber(UUID.randomUUID().toString().replace("-", ""));

        orders.setStatus(1);

        orders.setUserId(user.getId());


        //地址id不需要设置因为页面提供了
        orders.setOrderTime(LocalDateTime.now());

        orders.setCheckoutTime(LocalDateTime.now());


        orders.setPhone(user.getPhone());

        orders.setUserName("瑞吉用户"+user.getPhone());
        //查询数据库获取地址信息

        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());
        String adress = (addressBook.getProvinceName() != null ? addressBook.getProvinceName() : "") +
                (addressBook.getCityName() != null ? addressBook.getCityName() : "") +
                (addressBook.getDistrictName() != null ? addressBook.getDistrictName() : "") +
                (addressBook.getDetail() != null ? addressBook.getDetail() : "");

        orders.setAddress(adress);

        orders.setConsignee(addressBook.getConsignee());

        System.out.println("-----------------------------------" + orders);

        //往订单表里添加了数据
        int row = orderDao.insert(orders);

        //再往订单详情表里添加数据
        List<ShoppingCart> cartList = shoppingCartService.findList();
        int row2 = 0;
        for (ShoppingCart shoppingCart : cartList) {

            OrderDetail orderDetail = new OrderDetail();

            BeanUtils.copyProperties(shoppingCart, orderDetail);

            orderDetail.setOrderId(orders.getId());
            System.out.println(orderDetail.getOrderId());

            row2 += orderdetailDao.insert(orderDetail);
        }
        return row > 0 && row2 == cartList.size() ? 1 : 0;

    }



    @Override
    public IPage<OrdersDto> userPage(pageParm pageParm) {


        IPage<Orders> iPage = new Page<>(pageParm.getPage(), pageParm.getPageSize());

        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();

        lqw.orderByDesc(Orders::getOrderTime);
        User user = (User) session.getAttribute("user");
        lqw.eq(Orders::getUserId,user.getId());
        IPage<Orders> ordersIPage = orderDao.selectPage(iPage, lqw);

       List<OrdersDto> dtoList= ordersIPage.getRecords().stream().map(order ->{

           List<OrderDetail> detailList = orderdetailService.findByOrderId(order.getId());

            OrdersDto ordersDto = new OrdersDto();

           BeanUtils.copyProperties(order,ordersDto);

            ordersDto.setOrderDetails(detailList);



            return ordersDto;


        } ).collect(Collectors.toList());


       IPage<OrdersDto> dtoIPage=new Page<>();

       BeanUtils.copyProperties(ordersIPage,dtoIPage);
       dtoIPage.setRecords(dtoList);


       return dtoIPage;
    }

    @Override
    public IPage<Orders> findPage(pageParm pageParm) {
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();

        IPage<Orders> iPage= new Page<>(pageParm.getPage(), pageParm.getPageSize());

        lqw.like(pageParm.getNumber()!=null,Orders::getNumber,pageParm.getNumber());
        lqw.between(pageParm.getBeginTime()!=null, Orders::getOrderTime,pageParm.getBeginTime(),pageParm.getEndTime());
        return orderDao.selectPage(iPage,lqw);
    }
}
